//
//  ChooseFileTableViewCell.m
//  BDD_TWO
//
//  Created by XTW on 2023/6/9.
//


#import "ChooseFileTableViewCell.h"
#import "EZAudioFile.h"
#import "FileManager.h"
#import "AVPlayerManager.h"
#import "LocalRecordModel.h"

@interface ChooseFileTableViewCell()
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *desLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (nonatomic, strong) EZAudioFile *audioFile;
@property (weak, nonatomic) IBOutlet UIImageView *chooseIcon;
@property (weak, nonatomic) IBOutlet UIButton *playBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *playBtnLeading;

@end

@implementation ChooseFileTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.bgImageView setBackgroundColor:ViewBackgroundColor];
    self.bgImageView.layer.masksToBounds = YES;
    self.bgImageView.layer.cornerRadius = 8.0f;
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)playPress {
    if (self.ClickHandle) {
        self.ClickHandle(self.model);
    }
    self.playBtn.selected = !self.playBtn.selected;
    if (!self.playBtn.selected) {
        self.model.isPlaying = NO;
        [[AVPlayerManager manager] pause];
    } else {
        self.model.isPlaying = YES;
        [[AVPlayerManager manager] play];
    }
}

- (void)hiddenChooseIcon {
    self.chooseIcon.hidden = YES;
    self.playBtnLeading.constant = 28;
}

- (void)setModel:(LocalRecordModel *)model {
    _model = model;
    self.nameLabel.text = model.nickName;
    self.timeLabel.text = model.createTime;
    NSString *path = [[FileManager sharedManager] getLocalFilePath:model.fileName];
    if ([[FileManager sharedManager] judgeObjectExistence:path] && [[NSData dataWithContentsOfFile:path] length] > 0) {
        self.audioFile = [EZAudioFile audioFileWithURL:[NSURL fileURLWithPath:path]];
    } else {
        self.audioFile = nil;
    }
    self.desLabel.text = [NSString stringWithFormat:@"%@  %@  %@", self.audioFile.formattedDuration, [[FileManager sharedManager] getLocalFileSize:model.fileName], [model.fileName pathExtension]];
    if (model.isSelected) {
        self.chooseIcon.image = [UIImage systemImageNamed:@"circle.fill"];
    } else {
        self.chooseIcon.image = [UIImage systemImageNamed:@"circle"];
    }
    [self.chooseIcon setTintColor:ThemeColor];
    self.playBtn.selected = model.isPlaying;
}

@end
